import { searcher } from "@/api/other"

export default {
  namespaced: true,
  state: {
    lessonList: [],
    page: {
      current: 1,
      total: undefined,
      size: 12,
    }
  },
  mutations: {
    UPDATE_LESSON_LIST(state, lessonPage) {
      state.page.current = lessonPage.current;
      state.page.total = lessonPage.total;
      return state.lessonList = lessonPage.records;
    },

    UPDATE_PAGE_CURRENT(state, data) {
      return state.page.current = data;
    },
    
    UPDATE_PAGE_TOTAL(state, data) {
      return state.page.total = data;
    },

    UPDATE_PAGE_SIZE(state, data) {
      return state.page.size = data;
    }
  },
  actions: {
    setLessonList({ commit }, data) {
      return new Promise((resolve, reject) => {
        const subjectType = data.subjectType;
        const majorId = data.majorId;
        const pageNum = data.current;
        const pageSize = data.size;
        searcher({ subjectType, majorId, pageNum, pageSize }).then(res => {
          commit("UPDATE_LESSON_LIST", res)
          resolve(res);
        })
        
      })
    },

    setPageCurrent({ commit }, data) {
      commit("UPDATE_PAGE_CURRENT",data)
    },

    setPageTotal({ commit }, data) {
      commit("UPDATE_PAGE_TOTAL",data)
    },

    setPageSize({ commit }, data) {
      commit("UPDATE_PAGE_SIZE",data)
    } 
  },
  getters: {
    lessonList(state) {
      return state.lessonList;
    },

    current(state) {
      return state.page.current;
    },

    total(state) {
      return state.page.total;
    },

    size(state) {
      return state.page.size;
    }
  }
    
}